//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Notification;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using System.ComponentModel;

namespace OpenLS.Chart
{
    public class LegendEntry : ViewModelBase, IOfficeXmlSerializable
    {
        private ExtensionElement _extension;

        private int _idx;

        private TextBody _txPr;

        public bool Delete
        {
            get { return _txPr == null; }
        }

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value;
                notifyPropertyChanged("Extension"); }
        }

        public int Index
        {
            get { return _idx; }
            set
            {
                _idx = value;
                notifyPropertyChanged("Index");
            }
        }

        public TextBody TextProperties
        {
            get { return _txPr; }
            set { _txPr = value;
                notifyPropertyChanged("TextProperties"); }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("legendEntry"))
            {
                Index = c.ReadElementIntegerAttribute("idx");
                if (!c.ReadOptionalElementBooleanAttribute("delete", false))
                    TextProperties = c.ReadOptionalElementAs<TextBody>("txPr");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("legendEntry"))
            {
                c.WriteElementIntegerAttribute("idx", Index);
                if (TextProperties == null)
                    c.WriteElementBooleanAttribute("delete", "val", true);
                else
                    c.WriteElementAs(TextProperties, "txPr");
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

        public void SetDelete()
        {
            _txPr = null;
        }
    }
}